package com.capinfo.accounting.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.NativeWebRequest;
import com.capinfo.accumulation.model.accounting.mybist.GB_JZPZXX;
import com.capinfo.accumulation.model.accounting.mybist.HZJZPZ;
import com.capinfo.accumulation.model.pagemodel.Json;
import com.capinfo.accumulation.parameter.accounting.DataGrid;
import com.capinfo.accumulation.service.accounting.mybits.GB_JZPZXXServiceI;
import com.capinfo.accumulation.service.accounting.mybits.HZJZPZServiceI;
import com.capinfo.accumulation.service.accounting.mybits.XYRJZServiceI;
import com.capinfo.accumulation.util.JSONUtil;
import com.capinfo.commons.project.model.security.SystemUser;
import com.capinfo.components.security.web.utils.WebRequestUtils;
import com.google.gson.Gson;

/**
 * 现银凭证控制器
 * @author
 * 
 */
@Controller
@RequestMapping("/business/accounting/xYRJZ")
public class XYRJZController {
	
	//记账凭证
	@Autowired
	public GB_JZPZXXServiceI gB_JZPZXXService;
	
	//汇总凭证
	@Autowired
	HZJZPZServiceI hZJZPZService;
	
	@Autowired
	public XYRJZServiceI xYRJZServiceI;
	
	
	/**
	 * 记账凭证删除
	 * @param id
	 * @return
	 */
	@RequestMapping("/pzfldelete.shtml")
	@ResponseBody
	public Json pzfldelete(String id) {
		Json j = new Json();
		try {
			gB_JZPZXXService.deleteByPrimaryKey(id);
			j.setSuccess(true);
			j.setMsg("删除成功");
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}
	
	
	/**
	 * 添加分录保存
	 * @param t
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping("/bgadd.shtml")
	@ResponseBody
	public Json bgadd(GB_JZPZXX t,HttpSession session,NativeWebRequest request,String xspzh) {
		Json j = new Json();
		String entity=request.getParameter("inserted");
		List l=JSONUtil.toList(entity);
		 for (int i = 0; i < l.size(); i++) {
				Map map = (Map) l.get(i);
				try {
					t.setID(map.get("ID")==null||"".equals(map.get("ID"))?null:Long.parseLong((map.get("ID").toString()).replace(".0", "")));
					t.setHSDW(Long.parseLong(map.get("HSDW").toString().replace(".0", "")));
					t.setFJDJS(Long.parseLong(map.get("FJDJS").toString().replace(".0", "")));
					t.setZHAIYAO(map.get("ZHAIYAO").toString());
					t.setKMBH(map.get("KMBH").toString());
					try {
						t.setJFFSE(Double.parseDouble(map.get("JFFSE").toString()));
					} catch (Exception e) {}
					try {
						t.setDFFSE(Double.parseDouble(map.get("DFFSE").toString()));
					} catch (Exception e) {}
					t.setPZLB("2");
					t.setXSPZH(xspzh);
					
					if(t.getID()==null){
						gB_JZPZXXService.insert(t);
						j.setSuccess(true);
						j.setMsg("添加成功");
						j.setObj(t);
					}else{
						gB_JZPZXXService.updateByPrimaryKeySelective(t);	
						j.setSuccess(true);
						j.setMsg("修改成功");
						j.setObj(t);
					}
				} catch (Exception e) {
					e.printStackTrace();
					j.setMsg("操作失败");
				}
			}
		
		return j;
	}
	
	
	/**
	 * 跳转添加分录
	 * @return
	 */
	@RequestMapping("/tjfladd.shtml")
	public String tjfladd(String xspzh ,HttpServletRequest request) {
		GB_JZPZXX gb = new GB_JZPZXX();
		gb.setXSPZH(xspzh);
		request.setAttribute("gb", gb);
		return "/business/accounting/sgpzgl/tjfladd";
	}
	
	/**
	 * 记账凭证查询
	 * @param param
	 * @return
	 */
	@RequestMapping("/jzpzdataGrid.shtml")
	@ResponseBody
	public DataGrid jzpzdataGrid(@RequestParam  Map param) {
		return xYRJZServiceI.findGBByGrid(param);
	}
	
	/**
	 * 日记账查询
	 * @param param
	 * @return
	 */
	@RequestMapping("/dataGrid.shtml")
	@ResponseBody
	public DataGrid dataGrid(@RequestParam  Map param) {
		return xYRJZServiceI.findbyByGrid(param);
	}
	
	/**
	 * 现银凭证查询
	 * @param param
	 * @return
	 */
	@RequestMapping("/xydataGrid.shtml")
	@ResponseBody
	public DataGrid xydataGrid(@RequestParam  Map param) {
		return xYRJZServiceI.xyfindbyByGrid(param);
	}
	
	/**
	 * 现银凭证编辑保存
	 * @param t
	 * @param session
	 * @param request
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping("/edit.shtml")
	@ResponseBody
	public Json edit(HZJZPZ t , HttpSession session,NativeWebRequest request) throws ParseException {
		SystemUser user = (SystemUser)WebRequestUtils.retrieveSecureUser(request);
		Json j = new Json();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date date = sdf.parse(t.getRq());
		try {
			t.setPZLB("2");
			t.setJZRQ(date);
			t.setZD(user.getName());
			t.setZDRQ(new Timestamp(System.currentTimeMillis()));
			hZJZPZService.updateByPrimaryKeySelective(t);
			j.setSuccess(true);
			j.setMsg("修改成功");
			j.setObj(t);
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}
	
	/**
	 * 跳转凭证编辑页面
	 * @return
	 */
	@RequestMapping("/editPage.shtml")
	public String editPage(String id ,HttpServletRequest request) {
		HZJZPZ hZJZPZ = this.hZJZPZService.selectByPrimaryKey(id);
		request.setAttribute("hZJZPZ", hZJZPZ);
		return "/business/accounting/sgpzgl/sgxypzUpdate";
	}
	
	/**
	 * 跳转制作页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/addPage.shtml")
	public String addPage(HttpServletRequest request,double jffse,double dffse,String cnlsh) {
		HZJZPZ hZJZPZ  = new HZJZPZ();
		hZJZPZ.setJFFSE(jffse);
		hZJZPZ.setDFFSE(dffse);
		hZJZPZ.setZJLSH(cnlsh);
		request.setAttribute("hZJZPZ", hZJZPZ);
		return "/business/accounting/sgpzgl/sgpzAdd";
	}
	
	/**
	 * 现银凭证删除
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete.shtml")
	@ResponseBody
	public Json delete(String id) {
		Json j = new Json();
		try {
			hZJZPZService.deleteByPrimaryKey(id);
			j.setSuccess(true);
			j.setMsg("删除成功");
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}
	
	/**
	 * 跳转现银凭证查询
	 * @param request
	 * @return
	 */
	@RequestMapping("/xypz.shtml")
	public String xypz(HttpServletRequest request,String jzrq) {
		if (!jzrq.equals("")) {
			request.setAttribute("jzrq", jzrq);
		}
		return "/business/accounting/sgpzgl/XYPZ";
	}
	
	/**
	 * 添加方法
	 * @param t
	 * @param session
	 * @param request
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping("/add.shtml")
	@ResponseBody
	public Json add(HZJZPZ t , HttpSession session,NativeWebRequest request) throws ParseException {
		SystemUser user = (SystemUser)WebRequestUtils.retrieveSecureUser(request);
		Json j = new Json();
		Map dateMap = new HashMap();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date date = sdf.parse(t.getRq());
		try {
			SimpleDateFormat nyr = new SimpleDateFormat("yyyyMMdd");	//设置日期格式
			SimpleDateFormat ny = new SimpleDateFormat("yyyyMM");
			dateMap.put("nyr",nyr.format(new Date()));
			dateMap.put("ny",ny.format(new Date()));
			//查询凭证编号
			Map pzbh = xYRJZServiceI.pzbhcx(dateMap);
			if(pzbh==null){
				t.setXSPZH("00001");
			}else{
				t.setXSPZH(pzbh.get("XSPZH").toString());
			}
			t.setPZLB("2");
			t.setJZRQ(date);
			t.setZD(user.getName());
			t.setZDRQ(new Timestamp(System.currentTimeMillis()));
			hZJZPZService.insert(t);
			j.setSuccess(true);
			j.setMsg("添加成功");
			j.setObj(t);
		} catch (Exception e) {
			e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}
	
	/**
	 * 核算单位下拉
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value="/hsdw")  
	@ResponseBody  
	public void hsdw (HttpServletRequest request,HttpServletResponse response) throws IOException  {
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		Gson gson = new Gson();
		Map map = new HashMap();
		List<Map<String,Object>>hsdw= hZJZPZService.hsdw();
		PrintWriter pw;
		pw = response.getWriter();
		pw.write(gson.toJson(hsdw));
	}
	
	/**
	 * 科目编码下拉
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value="/kmbm")  
	@ResponseBody  
	public void kmbm (HttpServletRequest request,HttpServletResponse response) throws IOException  {
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		 Gson gson = new Gson();
		 Map map = new HashMap();
		 List<Map<String,Object>>kmbm= xYRJZServiceI.kmbm();
		 PrintWriter pw;
		 pw = response.getWriter();
		 pw.write(gson.toJson(kmbm));
	  }

}
